﻿using Gds.Model;
using Gds.Model.ConfigModel.LogModel;
using Gds.Utility.Helper;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;

namespace Gds.ViewModel.pageViewModel
{
    public class CommunicationLogViewModel:INotifyPropertyChanged
    {
        #region default param
        public static volatile CommunicationLogViewModel _instance = new CommunicationLogViewModel();
        public static object LockHelper = new object();
        public static object LogLockHelper = new object();
        public static CommunicationLogViewModel Instance
        {
            get
            {
                if (_instance == null)
                {
                    lock (LockHelper)
                    {
                        if (_instance == null)
                        {
                            _instance = new CommunicationLogViewModel();
                        }
                    }
                }
                return _instance;
            }
        }
        public event PropertyChangedEventHandler PropertyChanged;
        public virtual void OnPropertyChange([CallerMemberName] string propertyName = null)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
        #endregion

        #region LogShowModel
        private AsyncObservableCollection<Model.ConfigModel.LogModel.BasicLogModel> _communicationLogs;
        public AsyncObservableCollection<Model.ConfigModel.LogModel.BasicLogModel> CommunicationLogs
        {
            get { return _communicationLogs; }
            set { _communicationLogs = value; OnPropertyChange(); }
        }
        /// <summary>
        /// 添加日志
        /// </summary>
        /// <param name="msg"></param>
        public void AddLogModel(string msg,int _totalLine,int _controller)
        {
            try
            {
                if (CommunicationLogs == null) CommunicationLogs = new AsyncObservableCollection<BasicLogModel>();
                lock (LogLockHelper)
                {
                    CommunicationLogs.Add(new BasicLogModel()
                    {
                        LogIndex = CommunicationLogs.Count + 1,
                        LogTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
                        LogMsg = msg
                    });
                }
                if (CommunicationLogs.Count > 150)
                {
                    CommunicationLogs.Clear();
                }
                if(MainViewModel.Instance.IsSaveLog)
                {
                    PublicHelper.WriteControllerMsg(msg, _totalLine, _controller);
                }
                
            }
            catch (Exception ex)
            {
                PublicHelper.WriteErrorMsg(ex);
            }

        }
        #endregion

        #region realTimeData
        private AsyncObservableCollection<Model.ConfigModel.GasMeter.ShowRealTimeModel> _gasMeterRealTimeDatas;
        public AsyncObservableCollection<Model.ConfigModel.GasMeter.ShowRealTimeModel> GasMeterRealTimeDatas
        {
            get { return _gasMeterRealTimeDatas; }
            set { _gasMeterRealTimeDatas = value; OnPropertyChange(); }
        }
        #endregion
        #region task state Model
        private AsyncObservableCollection<Model.TestFunctionModel.TaskRunStateModel> _currentRunStateModels;
        public AsyncObservableCollection<Model.TestFunctionModel.TaskRunStateModel> CurrentRunStateModels
        {
            get { return _currentRunStateModels; }
            set { _currentRunStateModels = value;OnPropertyChange(); }
        }
        private Model.TestFunctionModel.TaskRunStateModel _currentRunStateModel;
        /// <summary>
        /// 当前选中的 显示的 状态
        /// </summary>
        public Model.TestFunctionModel.TaskRunStateModel CurrentRunStateModel
        {
            get { return _currentRunStateModel; }
            set
            {
                _currentRunStateModel = value;
                OnPropertyChange();
            }
        }
        #endregion 
        public List<string> FindTotalLineList()
        {
            List<string> result = new List<string>();
            foreach (var k in ViewModel.pageViewModel.MainViewModel.Instance.TaskStartModels)
            {
                result.Add(k.TotalLineNo.ToString());
            }
            return result;
        }
        public List<string> FindControllerAddressList(int totalLine)
        {
            List<string> result = new List<string>();
            foreach (var k in ViewModel.pageViewModel.MainViewModel.Instance.TaskStartModels)
            {
              if(k.TotalLineNo == totalLine)
                {
                    foreach (var w in k.Dic_Controller)
                    { 
                        result.Add(w.Value.Address.ToString());
                    }
                }
            }
            if (CurrentRunStateModels != null)
            { 
                foreach(var k in CurrentRunStateModels)
                {
                    if (k.TaskIndex == totalLine)
                    {
                        CurrentRunStateModel = k;
                        break;
                    }
                }
            }
            return result;
        }
    }
}
